package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Surface
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.navigation.AppBottomBarM3
import com.example.myapplication.navigation.AppHost
import com.example.myapplication.navigation.AppNavigationActions
import com.example.myapplication.navigation.AppRoute
import com.example.myapplication.navigation.FloatActionButtonM3
import com.example.myapplication.navigation.NavigationManager
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                AppEntry()
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppEntry(){
    val navController = rememberNavController()

    DisposableEffect(navController) {
        //设置全局的控制器
        NavigationManager.setNavController(navController)

        //销毁时选择将导航栏置为null
        onDispose {
            NavigationManager.setNavController(null)
        }
    }


    //bottomBarState表示底部tabBar的显示与隐藏
    val bottomBarState = rememberSaveable { mutableStateOf(value = true) }

    //fabState控制着floatActionButton的显示与隐藏
    val fabState = rememberSaveable { mutableStateOf(value = true) }

    //当前的返回堆栈
    val currentBackStateEntry by navController.currentBackStackEntryAsState()
    //返回堆栈的路径
    val selectedDestination = currentBackStateEntry?.destination?.route?:AppRoute.CONTROL

    //App导航到指定的目的地
    val navigationActions = remember(navController){
        AppNavigationActions(navController)
    }

    //判断哪些页面需要显示floatActionBtn，哪些页面需要显示底部导航栏，在这里统一进行规定
    when(currentBackStateEntry?.destination?.route){
        AppRoute.CONTROL_MAIN -> {
            fabState.value = true
            bottomBarState.value = true
        }
        AppRoute.SMART_MAIN -> {
            fabState.value = false
            bottomBarState.value = true
        }
        AppRoute.SHOP_MAIN -> {
            fabState.value = false
            bottomBarState.value = true
        }
        AppRoute.MINE_MAIN -> {
            fabState.value = false
            bottomBarState.value = true
        }
        AppRoute.ADD_ACCOUNT -> {
            fabState.value = false
            bottomBarState.value = false
        }
        else -> {
            fabState.value = false
            bottomBarState.value = false
        }
    }

    println("currentBackStateEntry.destination.route is ${currentBackStateEntry?.destination?.route}")


    Surface{
        Scaffold(
            bottomBar = {
                AppBottomBarM3(
                    selectedDestination = selectedDestination,
                    navigationToTopLevelDesination =  navigationActions::navigationTo,
                    bottomBarState = bottomBarState
                )},
            floatingActionButton = {
                FloatActionButtonM3(
                    fabState = fabState,
                    currentBackStack = currentBackStateEntry,
                    navigationToTopLevelDestination = { dest ->
                        navigationActions.navigationTo(dest)
                    },
//                   navigationToTopLevelDestination = navigationActions::navigationTo,
                )
            }
        ){ innerPadding ->
            Column(modifier = Modifier.padding(innerPadding).fillMaxSize()) {
                AppHost(navController = navController)
            }
        }
    }
}



